package com.si.anddos;

/**
 * 
 * @author Carlos Martinez
 *
 */

import java.io.File;

import com.si.anddos.dpadapters.AnddosDbAdapter;

import android.content.Intent;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

/**
 * A class representing Main activity in anddos application, this is the start
 * activity
 * 
 * @author Carlos Martinez
 * 
 */
public class MainActivity extends OptionsMenuActivity {

	private Button buttonDownloadList;
	private Button buttonUploadList;
	private Button buttonAccountList;
	private Button buttonDownload;
	private Button buttonUpload;

	private EditText editTextUrl;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);

		Log.v("Storage directory",Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).toString());
		File defaultDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
		defaultDirectory.mkdirs();
		
		editTextUrl = (EditText) findViewById(R.id.editTextUrl);

		buttonDownloadList = (Button) findViewById(R.id.buttonDownloadList);
		buttonDownloadList.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				startDownloadList();
			}
		});

		buttonUploadList = (Button) findViewById(R.id.buttonUploadList);
		buttonUploadList.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				startUploadList();
			}
		});

		// button to new download click listener
		buttonDownload = (Button) findViewById(R.id.buttonDownload);
		buttonDownload.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				startNewDownload();
			}
		});
		
		// button to new upload click listener
		buttonUpload = (Button) findViewById(R.id.buttonUpload);
		buttonUpload.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				startNewUpload();
			}
		});
		
		// button to account list click listener
		buttonAccountList = (Button) findViewById(R.id.buttonAccountList);
		buttonAccountList.setOnClickListener(new View.OnClickListener() {
			public void onClick(View view) {
				startAccountList();
			}
		});

	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		// add new menu item to Options menu - other items are defined in
		// OptionsMenuActivity
		menu.add(0, Globals.SEARCH_ID, 0, R.string.menuSearch).setIcon(android.R.drawable.ic_menu_search);
		return true;
	}
	
	@Override
    public boolean onMenuItemSelected(int featureId, MenuItem item) {
    	//handling menu items click
        switch(item.getItemId()) {
            case Globals.SEARCH_ID:
            	// start search activity
            	Intent searchActivity = new Intent(this, SearchActivity.class);
            	startActivity(searchActivity);
                return true;
        }

        return super.onMenuItemSelected(featureId, item);
    }
	private void startAccountList() {
		// start activity for list of accounts
		Intent i = new Intent(this, AccountListActivity.class);
		startActivity(i);
	}

	private void startDownloadList() {
		// start activity for list of downloads
		Intent i = new Intent(this, DownloadListActivity.class);
		startActivity(i);
	}

	private void startNewDownload() {
		// start activity for new download
		Intent i = new Intent(this, DownloadNewActivity.class);
		i.putExtra("url", editTextUrl.getText().toString());
		startActivity(i);
	}
	
	private void startUploadList() {
		// start activity for list of uploads
		Intent i = new Intent(this, UploadListActivity.class);
		startActivity(i);
		
	}
	
	private void startNewUpload() {
		Intent i = new Intent(this, UploadNewActivity.class);
        startActivity(i);
	}
	
}